﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;
using TomNet.App.Core.Contracts;
using TomNet.App.Model.DB;
using TomNet.App.Model.DTO;
using TomNet.AspNetCore.Data;
using TomNet.SqlSugarCore.Entity;
using TomNet.Security.Claims;
using System.Security.Claims;
using TomNet.App.Model.DB.Identity;

namespace TomNet.App.Web.Controllers
{
    [Authorize]
    [Description("奖励订单管理")]
    public class UserRewardBusinessController : Controller
    {
        private readonly IUserRewardBusinessContract _userRewardBusinessContract;        
        private readonly IUnitOfWork _unitOfWork;
        public UserRewardBusinessController(IUserRewardBusinessContract userRewardBusinessContract,            
            IUnitOfWork unitOfWork)
        {
            _userRewardBusinessContract = userRewardBusinessContract;
            _unitOfWork = unitOfWork;
        }
        [Description("奖励订单列表")]
        [HttpGet]
        public IActionResult UserRewardBusinessList()
        {
            return View();
        }
        [Description("奖励订单列表")]
        [HttpPost]
        public IActionResult UserRewardBusinessList(int page = 1, int limit = 20)
        {
            var query = _userRewardBusinessContract.UnitOfWork.DbContext.Queryable<UserRewardBusiness, Business, Users>((ub, b, u)
                   => ub.UserID == u.Id && ub.BusinessID == b.Id)
                //.Where((ub, b, u) => r.Id == intType)
                .OrderBy((ub, b, u) => ub.Id, OrderByType.Desc)
               .Select((ub, b, u) => new UserRewardBusinessDto
               {
                   UserName = u.UserName,
                   UserID = u.Id,
                   Hashrate = ub.Hashrate,
                   Kwh = ub.Kwh,
                   BeginTime = ub.BeginTime,
                   EndTime = ub.EndTime,
                   CreatedTime = ub.CreatedTime,
                   Remark = ub.Remark
               });
            int total = 0;
            var list = query.ToPageList(page, limit, ref total);
            var result = new LayuiPageResult<UserRewardBusinessDto>
            {
                Code = 0,
                Msg = "Success",
                Count = total,
                Data = list.ToArray()
            };
            return Json(result);
        }
        [Description("奖励订单添加")]
        [HttpGet]
        public IActionResult UserRewardBusinessInsert()
        {
            return View();
        }
        [Description("奖励订单添加")]
        [HttpPost]
        public IActionResult UserRewardBusinessInsert(UserRewardBusiness userRewardBusiness)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            var count = _userRewardBusinessContract.Entities.AsQueryable().Where(m => m.BusinessID == userRewardBusiness.BusinessID).Count();
            if (count > 0)
            {
                result.Content = "订单已经存在";
            }
            else
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
                _userRewardBusinessContract.Insert(userRewardBusiness);
            }

            return Json(result);
        }
        [Description("奖励订单修改")]
        [HttpGet]
        public IActionResult UserRewardBusinessEdit(int id)
        {
            var goods = _userRewardBusinessContract.Entities.AsQueryable().Where(m => m.Id == id).First();
            return View(goods);
        }
        [Description("奖励订单修改")]
        [HttpPost]
        public IActionResult UserRewardBusinessEdit(UserRewardBusiness userRewardBussiness)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            var count = _userRewardBusinessContract.Entities.AsQueryable().Where(m => m.BusinessID == userRewardBussiness.BusinessID && m.Id != userRewardBussiness.Id).Count();
            if (count > 0)
            {
                result.Content = "订单已经存在";
            }
            else
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
                _userRewardBusinessContract.Update(userRewardBussiness);
            }
            return Json(result);
        }
        [Description("奖励订单删除")]
        [HttpPost]
        public IActionResult UserRewardBusinessDelete(int id)
        {
            AjaxResult result = new AjaxResult { Type = Data.AjaxResultType.Error, Content = "未知异常", Data = null };
            int count = _userRewardBusinessContract.Delete(id);
            if (count > 0)
            {
                result.Content = "操作成功";
                result.Type = Data.AjaxResultType.Success;
            }
            else
            {
                result.Content = "操作失败";
                result.Type = Data.AjaxResultType.Error;
            }
            return Json(result);
        }
    }
}